/**
 * 时间格式化，完整
 * @param {*} date 
 */
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hours = date.getHours()
  const minutes = date.getMinutes()
  const seconds = date.getSeconds()

  let formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds);
  return formattedDate
}
//时间格式化，日期
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  let formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
  return formattedDate
}
//时间格式化，时间
const dayTime = date => {
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[hour, minute, second].map(formatNumber).join(':')}`
}
//日期
const afterDay = (date, afterDay) => {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate() + afterDay
  // 处理日的进位
  let daysInMonth = new Date(year, month, 0).getDate(); // 获取当月的天数
  if (day > daysInMonth) {
    day -= daysInMonth;
    month += 1;
    if (month > 12) {
      month = 1;
      year += 1;
    }
  }

  let formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
  return formattedDate
}
//预约时间分
const afterMinute = (date, afterMinutes) => {
  let hour = date.getHours()
  let minute = date.getMinutes() + afterMinutes
  const second = date.getSeconds()
  // 向上取整到最接近的10的倍数
  minute = Math.ceil(minute / 10) * 10;

  // 处理进位
  if (minute === 60) {
    minute = 0;
    hour += 1;
    hour = hour % 24; // 如果小时超过24，则将小时重置为0
  }

  return `${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}


// 计算两点之间的距离（单位：米）
function getDistance(lat1, lon1, lat2, lon2) {
  var R = 6371000; // 地球半径，单位米
  var dLat = (lat2 - lat1) * Math.PI / 180;
  var dLon = (lon2 - lon1) * Math.PI / 180;
  var a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var distance = R * c;
  return distance;
}
/**
 * 提取地址省、市、县、区
 * @param {地址} address 
 */
function extAddr(address) {
  // 定义省、市、县/区的变量
  let province = '';
  let city = '';
  let district = '';
  // 使用正则表达式匹配地址字符串
  const regex = /(.*?省|.*?自治区|.*?行政区|.*?特别行政区)?(.*?市|.*?地区|.*?自治州)?(.*?区|.*?县|.*?旗|.*?市|.*?镇|.*?乡|.*?街道)?/;
  const matches = address.match(regex);

  // 如果有匹配项，则提取省、市、县/区
  if (matches && matches.length > 0) {
    province = matches[1] || '';
    city = matches[2] || '';
    district = matches[3] || '';
  }

  // 拼接省、市、县/区，并用斜杠分隔
  const result = `${province}/${city}/${district}`;

  return result;
}


module.exports = {
  formatTime,
  formatDate,
  dayTime,
  afterDay,
  afterMinute,
  extAddr,
  getDistance
}